# import asyncio


# async def my_task(m):
#     await asyncio.sleep(1)
#     return f"Task completed :{m}"


# if __name__ == "__main__":
#     coroutine20 = my_task(20)
#     coroutine40 = my_task(40)
#     loop = asyncio.get_event_loop()
#     tasks = [loop.create_task(coroutine20), loop.create_task(coroutine40)]
#     loop.run_until_complete(asyncio.gather(*tasks))
#     while tasks[0].done() & tasks[1].done():
#         print(tasks[0].result())
#         print(tasks[1].result())
#         break


# import asyncio


# async def my_task(m):
#     # await asyncio.sleep(1)
#     return f"Task completed :{m}"


# if __name__ == "__main__":
#     # 使用 asyncio.create_task 替代旧的 loop.create_task
#     coroutine20 = my_task(20)
#     coroutine40 = my_task(40)
#     tasks = [coroutine20, coroutine40]  # 直接使用协程对象

#     # asyncio.run 替代 get_event_loop 和 run_until_complete
#     completed_tasks = asyncio.run(asyncio.gather(*tasks))

#     # 打印所有任务的结果
#     for task_result in completed_tasks:
#         print(task_result)


# async def main():
#     task = asyncio.create_task(my_task())
#     await asyncio.sleep(0.5)  # 模拟等待一段时间
#     print(f"Task state: {task.state}")  # 可能显示 PENDING 或 RUNNING
#     await task
#     print(f"Task state: {task.state}")  # 显示 DONE
#     print(f"Task result: {task.result()}")


# asyncio.run(main())

import asyncio


async def my_task(m):
    await asyncio.sleep(1)
    return f"Task completed :{m}"


async def main():
    coroutine20 = my_task(20)
    coroutine40 = my_task(40)
    tasks = [asyncio.create_task(coroutine20), asyncio.create_task(coroutine40)]

    # await asyncio.wait(tasks）返回的是任务
    dones, _ = await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
    for task in dones:
        print(task.result())

    # await asyncio.gather(*tasks)返回的是任务的返回值
    # result = await asyncio.gather(*tasks)
    # for task in result:
    #     print(task)


if __name__ == "__main__":
    asyncio.run(main())
